﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace SimpoBudgetDataModel
{
    public class PaycheckList : Collection<Paycheck>
    {
        public event EventHandler<PaycheckChangedEventArgs> Changed;

        protected override void InsertItem(int index, Paycheck Paycheck)
        {
            base.InsertItem(index, Paycheck);

            EventHandler<PaycheckChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new PaycheckChangedEventArgs(
                    ChangeType.Added, Paycheck, null));
            }
        }

        protected override void SetItem(int index, Paycheck Paycheck)
        {
            Paycheck replaced = Items[index];
            base.SetItem(index, Paycheck);

            EventHandler<PaycheckChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new PaycheckChangedEventArgs(
                    ChangeType.Replaced, replaced, Paycheck));
            }
        }

        protected override void RemoveItem(int index)
        {
            Paycheck removedItem = Items[index];
            base.RemoveItem(index);

            EventHandler<PaycheckChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new PaycheckChangedEventArgs(
                    ChangeType.Removed, removedItem, null));

            }
        }

        protected override void ClearItems()
        {
            
            base.ClearItems();

            EventHandler<PaycheckChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new PaycheckChangedEventArgs(ChangeType.Cleared, null, null));

            }
        }

        public class PaycheckChangedEventArgs : EventArgs
        {
            public readonly Paycheck ChangedItem;
            public readonly ChangeType ChangeType;
            public readonly Paycheck ReplacedWith;

            public PaycheckChangedEventArgs(ChangeType change, Paycheck Paycheck, Paycheck replacement)
           {
               ChangeType = change;
               ChangedItem = Paycheck;
               ReplacedWith = replacement;
           }
        }

        public enum ChangeType
        {
            Added,
            Removed,
            Replaced,
            Cleared
        };
        
    }
}
